using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;


namespace Emlyn.Applications.Scratch.Graphics.Tools
{
    public abstract class ESprite: IDisposable   
    {
        DirectXWrapper _wrapper = null;
        private Size _spriteSize;

        public ESprite(DirectXWrapper aWrapper, Size aSize)
        {
            ResetDirectXWrapper(aWrapper, aSize);

            _pos = new Point(0, 0);
            _spriteSize = new Size(20, 20);
            _currentFrame = 0;
            _framesPerRow = 1;

            _spriteSize.Width = aSize.Width;
            _spriteSize.Height = aSize.Height;
        }

        private Point _pos;

        public Point Pos
        {
            get { return _pos; }
            set { _pos = value; }
        }
	
        private int _speedX;

        public int SpeedX
        {
            get { return _speedX; }
            set { _speedX = value; }
        }

        private int _speedY;

        public int SpeedY
        {
            get { return _speedY; }
            set { _speedY = value; }
        }

        public void IncrementPos()
        {
            Pos = new Point(
                Pos.X + SpeedX,
                Pos.Y + SpeedY
                );
        }

        public void DecrementPos()
        {
            Pos = new Point(
                Pos.X - SpeedX,
                Pos.Y - SpeedY
                );
        }

        public Size SpriteSize
        {
            get { return _spriteSize; }
            set { _spriteSize = value; }
        }

        private Sprite _systemSprite;

        public Sprite SystemSprite
        {
            get { return _systemSprite; }
            set { _systemSprite = value; }
        }

        private int _currentFrame;

        public int CurrentFrame
        {
            get { return _currentFrame; }
            set { _currentFrame = value; }
        }

        private int _framesPerRow;

        public int FramesPerRow
        {
            get { return _framesPerRow; }
            set { _framesPerRow = value; }
        }


        public void Draw(int aScrollX, int aScrollY)
        {
            PointF lfposition = new PointF(Pos.X-aScrollX, Pos.Y-aScrollY);
            Vector2 lscaleVector = new Vector2(1, 1);
            Vector2 lrotateVector = new Vector2(0, 0);
            Rectangle ltextrect = new Rectangle(
                (CurrentFrame % FramesPerRow) * SpriteSize.Width,
                (CurrentFrame / FramesPerRow) * SpriteSize.Height,
                SpriteSize.Width,
                SpriteSize.Height
                );
            SizeF ldestSize = new SizeF(
                ltextrect.Size.Width,
                ltextrect.Size.Height
                );

            _systemSprite.Begin(SpriteFlags.AlphaBlend);
            try
            {
                Texture ltexture = GetCurrentTexture();
                _systemSprite.Draw2D(ltexture, ltextrect, ldestSize, lfposition, Color.FromArgb(255, 255, 255, 255));                
            }
            finally
            {
                _systemSprite.End();
            }
        }

        public void ResetDirectXWrapper(            
            DirectXWrapper aWrapper,
            Size aSize
            )
        {
            _wrapper = aWrapper;
            _spriteSize = aSize;
            if (_wrapper != null)
            {
                _systemSprite = new Sprite(_wrapper._device);
            }
            else
            {
                _systemSprite = null;
            }
        }

        protected abstract Texture GetCurrentTexture();

        #region IDisposable Members

        public void Dispose()
        {
        }

        #endregion
    }
}
